package cn.gp.controller;

import cn.gp.common.PageInfo;
import cn.gp.common.R;
import cn.gp.entity.Consumer;
import cn.gp.service.ConsumerService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ConsumerController
 * @Description TODO
 * @Author He_Guoping
 * @Date 2023/5/28 12:59
 * @Version 1.0
 */
@Log4j
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;

    @GetMapping("/page")
    public R<PageInfo<Consumer>> page(Integer page, Integer pageSize, String name) {
        log.info("page: " + page + ", " + pageSize);
        return R.success(consumerService.page(page, pageSize, name));
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody Consumer consumer) {
        log.info("add: " + consumer);
        return consumerService.save(consumer) ? R.success("success") : R.error("error");
    }

    @DeleteMapping("/del/{id}")
    public R<String> del(@PathVariable Integer id) {
        log.info("del: " + id);
        return consumerService.deleteById(id) ? R.success("success") : R.error("error");
    }

    @PutMapping("/update")
    public R<String> update(@RequestBody Consumer consumer) {
        log.info("update: " + consumer);
        return consumerService.update(consumer) ? R.success("success") : R.error("error");
    }

}
